/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.ana.teste;

import br.gov.ana.entity.Historico;
import br.gov.ana.entity.TempoConcentracao;
import br.gov.ana.msg.Mensagem;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 *
 * @author rafael.rodrigues
 */
public class MainAlterarTC {
    
    public static void main(String[] args){
        
        try {
            List<String> listaTC = new ArrayList<String>();
            List<String> listaTCResult = new ArrayList<String>();
            
            BufferedReader inTC = new BufferedReader(new FileReader("C:\\tc\\arquivoTc\\tc.txt"));      
            
            if(inTC ==  null){
                Mensagem msg = new Mensagem();
                msg.run(new Mensagem(), 500, 150);
            }
            
            String str;
            String valores = "";
            
            int i = 0;
            while (inTC.ready()) {
                str = inTC.readLine();
                
                if(i >= 6){
                    valores += str;
                }
                i++;
            }
            
            listaTC = calculaTempoConcentracao(valores);
            
            int indice =0;
            int aux = 781;
            for(String d : listaTC){
                
                listaTCResult.add(d);
                
                if(indice == aux){
                    listaTCResult.add("-9999");
                    
                    aux = indice+782;
                }
                indice++;
            }
            
            gravarEmArquivo(listaTCResult);            
            
        } catch (IOException e) {
            
        }
    }
    
    public static void gravarEmArquivo(List<String> listaTCResult) throws IOException{
        
        FileWriter fileWriter = new FileWriter("C:\\tc\\cargaTC.txt", true);
        PrintWriter printWriter = new PrintWriter(fileWriter);
                
        for(String n1 : listaTCResult){
            printWriter.print(n1+" ");
        }
        
        printWriter.flush();
        printWriter.close();
    }
    
    public static List<String> calculaTempoConcentracao(String valores){
        
        List<String> listaTC = new ArrayList<String>();
        String[] d = valores.split(Pattern.quote(" "));  
        
        for(String valor : d){  
            //double valor2 = Double.parseDouble(valor.replace(",", "."));
            //double result = arredonda(valor2);
            
            //if(result == -9999){
              //  result = 0;
           // }
            listaTC.add(valor);
        }
        return listaTC;                
    }
    
    public static double arredonda(double numero){  
        
        //double multiplicao = numero*2;
        //double valor = Math.ceil(multiplicao);
        double valor = Math.ceil(numero);
        //double result = valor/2;  
        
        return valor;
    }  
}
